<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单四层级联选择器演示 - 基于 CascaderSelect.vue</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .demo-container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 16px;
            padding: 32px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        }

        .demo-header {
            text-align: center;
            margin-bottom: 32px;
            padding-bottom: 24px;
            border-bottom: 2px solid #f1f3f5;
        }

        .demo-header h1 {
            color: #2c3e50;
            font-size: 2rem;
            margin-bottom: 8px;
            font-weight: 700;
        }

        .demo-header p {
            color: #6c757d;
            font-size: 1.1rem;
        }

        .cascader-section {
            margin-bottom: 32px;
        }

        .section-title {
            margin-bottom: 16px;
            color: #374151;
            font-size: 1.2rem;
            font-weight: 600;
        }

        .cascader-container {
            margin-bottom: 24px;
        }

        .cascader-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #4b5563;
        }

        /* 级联选择器样式 - 与 CascaderSelect.vue 保持一致 */
        .cascader-select {
            position: relative;
            width: 100%;
        }
        
        .input-wrapper {
            position: relative;
            cursor: pointer;
        }
        
        .cascader-input {
            width: 100%;
            padding: 12px 60px 12px 16px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            background-color: #fff;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            line-height: 1.5;
            box-sizing: border-box;
        }
        
        .cascader-input:hover {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .cascader-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
        }
        
        .input-icons {
            position: absolute;
            right: 12px;
            top: 50%;
            transform: translateY(-50%);
            display: flex;
            align-items: center;
            gap: 4px;
        }
        
        .clear-icon {
            color: #9ca3af;
            font-size: 14px;
            cursor: pointer;
            padding: 2px;
            border-radius: 3px;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 16px;
            height: 16px;
        }
        
        .clear-icon:hover {
            color: #6b7280;
            background-color: #f3f4f6;
        }
        
        .arrow-icon {
            color: #adb5bd;
            font-size: 12px;
            transition: transform 0.3s ease;
            pointer-events: none;
            margin-left: 4px;
        }
        
        .arrow-icon.arrow-up {
            transform: rotate(180deg);
        }
        
        .cascader-dropdown {
            position: absolute;
            top: 100%;
            left: 0;
            right: 0;
            background: white;
            border: 2px solid #e9ecef;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
            z-index: 2000;
            margin-top: 8px;
            max-height: 450px;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .cascader-content {
            flex: 1;
            overflow: hidden;
        }
        
        .cascader-header {
            display: flex;
            border-bottom: 2px solid #e9ecef;
            background: #fff;
        }
        
        .cascader-header-item {
            flex: 1;
            min-width: 100px;
            width: 100px;
            padding: 8px 12px;
            font-weight: 600;
            font-size: 13px;
            color: #667eea;
            text-align: left;
            border-right: 1px solid #dee2e6;
            background: #fff;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        }
        
        .cascader-header-item:last-child {
            border-right: none;
        }
        
        .cascader-panels {
            display: flex;
            max-height: 350px;
            overflow: auto;
        }
        
        .cascader-panel {
            flex: 1;
            min-width: 100px;
            width: 100px;
            border-right: 1px solid #e9ecef;
            background: #fff;
        }
        
        .cascader-panel:last-child {
            border-right: none;
        }
        
        .cascader-option {
            padding: 8px 12px;
            cursor: pointer;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            color: #495057;
            transition: all 0.2s ease;
            position: relative;
        }
        
        .cascader-option:hover {
            background-color: #f8f9fa;
            color: #667eea;
        }
        
        .cascader-option.is-active {
            color: #667eea;
            background-color: #e7f3ff;
            font-weight: 500;
        }
        
        .cascader-option.is-selected {
            color: #667eea;
            font-weight: 600;
        }
        
        .cascader-option.has-children {
            padding-right: 40px;
        }
        
        .option-label {
            flex: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        
        .option-arrow {
            color: #adb5bd;
            font-size: 12px;
            margin-left: 8px;
            font-weight: bold;
        }
        
        .cascader-footer {
            border-top: 1px solid #e9ecef;
            padding: 16px 20px;
            display: flex;
            justify-content: flex-end;
            gap: 12px;
            background: #fff;
        }
        
        .btn {
            padding: 8px 20px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            border: 2px solid;
            transition: all 0.3s ease;
        }
        
        .btn-cancel {
            background: #fff;
            color: #6c757d;
            border-color: #dee2e6;
        }
        
        .btn-cancel:hover {
            color: #495057;
            border-color: #adb5bd;
            background-color: #f8f9fa;
        }
        
        .btn-confirm {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #fff;
            border-color: transparent;
        }
        
        .btn-confirm:hover:not(:disabled) {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }
        
        .btn-confirm:disabled {
            background: #adb5bd;
            border-color: #adb5bd;
            cursor: not-allowed;
            transform: none;
        }

        /* 选中信息展示 */
        .selection-info {
            background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
            border-radius: 12px;
            padding: 20px;
            margin-top: 24px;
            border: 1px solid #bae6fd;
        }

        .selection-info h3 {
            margin: 0 0 16px 0;
            color: #0369a1;
            font-size: 1.1rem;
            font-weight: 600;
        }

        .info-list {
            display: grid;
            gap: 12px;
        }

        .info-item {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .info-label {
            min-width: 80px;
            font-weight: 500;
            color: #475569;
        }

        .info-value {
            color: #1e293b;
            font-weight: 500;
        }

        .info-value.highlight {
            background: #fef3c7;
            color: #92400e;
            padding: 2px 8px;
            border-radius: 4px;
            font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
            font-weight: 600;
        }

        /* 层级展示 */
        .level-display {
            margin-top: 16px;
            padding-top: 16px;
            border-top: 1px solid #cbd5e1;
        }

        .level-display h4 {
            margin: 0 0 12px 0;
            color: #374151;
            font-size: 1rem;
        }

        .level-path {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            align-items: center;
        }

        .level-item {
            display: flex;
            align-items: center;
            gap: 4px;
            padding: 4px 8px;
            background: white;
            border: 1px solid #cbd5e1;
            border-radius: 6px;
            font-size: 0.9rem;
        }

        .level-number {
            background: #3b82f6;
            color: white;
            padding: 1px 6px;
            border-radius: 3px;
            font-size: 0.8rem;
            font-weight: 600;
        }

        .level-arrow {
            color: #9ca3af;
            font-weight: bold;
        }

        .empty-state {
            text-align: center;
            color: #9ca3af;
            font-style: italic;
            padding: 40px 20px;
        }

        .empty-icon {
            font-size: 2.5rem;
            margin-bottom: 8px;
            opacity: 0.6;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .demo-container {
                margin: 0 10px;
                padding: 20px;
            }
            
            .demo-header h1 {
                font-size: 1.6rem;
            }
            
            .level-path {
                flex-direction: column;
                align-items: flex-start;
            }
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="demo-container">
            <!-- 演示头部 -->
            <div class="demo-header">
                <h1>🔗 四层级联选择器演示</h1>
                <p>基于 CascaderSelect.vue 的简单演示，支持四层深度选择</p>
            </div>

            <!-- 级联选择器区域 -->
            <div class="cascader-section">
                <h2 class="section-title">📋 级联选择器</h2>
                
                <div class="cascader-container">
                    <label class="cascader-label">选择组件分类（支持四层深度）:</label>
                    <cascader-select
                        :model-value="selectedValue"
                        :options="fourLevelOptions"
                        placeholder="请选择组件分类..."
                        :header-labels="headerLabels"
                        @update:model-value="updateSelectedValue"
                        @change="handleCascaderChange"
                    ></cascader-select>
                </div>

                <!-- 选中信息展示 -->
                <div v-if="selectedValue.length > 0" class="selection-info">
                    <h3>🎯 当前选中信息</h3>
                    <div class="info-list">
                        <div class="info-item">
                            <span class="info-label">选择路径:</span>
                            <span class="info-value">{{ selectedLabels.join(' → ') }}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">选择层级:</span>
                            <span class="info-value">第 {{ selectedValue.length }} 层 / 共 4 层</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">最深值:</span>
                            <span class="info-value highlight">{{ requestParam }}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">完整值:</span>
                            <span class="info-value">{{ JSON.stringify(selectedValue) }}</span>
                        </div>
                    </div>

                    <!-- 层级展示 -->
                    <div class="level-display">
                        <h4>📊 层级路径</h4>
                        <div class="level-path">
                            <div 
                                v-for="(value, index) in selectedValue" 
                                :key="index"
                                class="level-item"
                            >
                                <span class="level-number">{{ index + 1 }}</span>
                                <span>{{ headerLabels[index] }}</span>
                                <strong>{{ selectedLabels[index] }}</strong>
                                <code>({{ value }})</code>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 空状态 -->
                <div v-else class="empty-state">
                    <div class="empty-icon">🎯</div>
                    <div>请选择组件分类</div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const { createApp, ref, computed } = Vue

        // 级联选择器组件 - 与 CascaderSelect.vue 保持一致
        const CascaderSelect = {
            props: {
                options: {
                    type: Array,
                    required: true
                },
                modelValue: {
                    type: Array,
                    default: () => []
                },
                placeholder: {
                    type: String,
                    default: '请选择'
                },
                headerLabels: {
                    type: Array,
                    default: () => ['分类', '子分类', '组件', '详细']
                }
            },
            emits: ['update:modelValue', 'change'],
            setup(props, { emit }) {
                const showDropdown = ref(false)
                const tempSelectedPath = ref([...props.modelValue])
                
                const cascaderPanels = computed(() => {
                    const panels = [props.options]
                    
                    let currentOptions = props.options
                    for (let i = 0; i < tempSelectedPath.value.length; i++) {
                        const selectedValue = tempSelectedPath.value[i]
                        const selectedOption = currentOptions.find(option => option.name === selectedValue)
                        
                        if (selectedOption && selectedOption.children) {
                            panels.push(selectedOption.children)
                            currentOptions = selectedOption.children
                        } else {
                            break
                        }
                    }
                    
                    return panels
                })
                
                const displayValue = computed(() => {
                    if (!props.modelValue.length) return ''
                    
                    let currentOptions = props.options
                    let finalLabel = ''
                    
                    for (const value of props.modelValue) {
                        const option = currentOptions.find(opt => opt.name === value)
                        if (option) {
                            finalLabel = option.nameCn
                            currentOptions = option.children || []
                        }
                    }
                    
                    return finalLabel
                })
                
                const toggleDropdown = () => {
                    showDropdown.value = !showDropdown.value
                    if (showDropdown.value) {
                        tempSelectedPath.value = [...props.modelValue]
                    }
                }
                
                const isOptionActive = (option, level) => {
                    return tempSelectedPath.value[level] === option.name
                }
                
                const handleOptionClick = (option, level) => {
                    const newPath = tempSelectedPath.value.slice(0, level)
                    newPath.push(option.name)
                    tempSelectedPath.value = newPath
                }
                
                const handleConfirm = () => {
                    const selectedOptions = []
                    let currentOptions = props.options
                    
                    for (const value of tempSelectedPath.value) {
                        const option = currentOptions.find(opt => opt.name === value)
                        if (option) {
                            selectedOptions.push(option)
                            currentOptions = option.children || []
                        }
                    }
                    
                    emit('update:modelValue', [...tempSelectedPath.value])
                    emit('change', [...tempSelectedPath.value], selectedOptions)
                    
                    showDropdown.value = false
                }
                
                const handleCancel = () => {
                    tempSelectedPath.value = [...props.modelValue]
                    showDropdown.value = false
                }
                
                const getHeaderLabel = (level) => {
                    return props.headerLabels[level] || `级别 ${level + 1}`
                }
                
                const handleClear = () => {
                    emit('update:modelValue', [])
                    emit('change', [], [])
                    tempSelectedPath.value = []
                    showDropdown.value = false
                }
                
                return {
                    showDropdown,
                    tempSelectedPath,
                    cascaderPanels,
                    displayValue,
                    toggleDropdown,
                    isOptionActive,
                    handleOptionClick,
                    handleConfirm,
                    handleCancel,
                    handleClear,
                    getHeaderLabel
                }
            },
            template: `
                <div class="cascader-select">
                    <div class="input-wrapper" @click="toggleDropdown">
                        <input
                            :value="displayValue"
                            :placeholder="placeholder"
                            readonly
                            class="cascader-input"
                        />
                        <div class="input-icons">
                            <span 
                                v-if="modelValue.length > 0"
                                class="clear-icon"
                                @click.stop="handleClear"
                                title="清空选择"
                            >
                                ×
                            </span>
                            <span 
                                class="arrow-icon"
                                :class="{ 'arrow-up': showDropdown }"
                            >
                                ▼
                            </span>
                        </div>
                    </div>

                    <div 
                        v-show="showDropdown" 
                        class="cascader-dropdown"
                    >
                        <div class="cascader-content">
                            <div class="cascader-header">
                                <div 
                                    v-for="(panel, level) in cascaderPanels"
                                    :key="'header-' + level"
                                    class="cascader-header-item"
                                >
                                    {{ getHeaderLabel(level) }}
                                </div>
                            </div>
                            <div class="cascader-panels">
                                <div
                                    v-for="(panel, level) in cascaderPanels"
                                    :key="level"
                                    class="cascader-panel"
                                >
                                    <div
                                        v-for="option in panel"
                                        :key="option.name"
                                        class="cascader-option"
                                        :class="{
                                            'is-active': isOptionActive(option, level),
                                            'has-children': option.children && option.children.length > 0
                                        }"
                                        @click="handleOptionClick(option, level)"
                                    >
                                        <span class="option-label">{{ option.nameCn }}</span>
                                        <span 
                                            v-if="option.children && option.children.length > 0"
                                            class="option-arrow"
                                        >
                                            ＞
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="cascader-footer">
                            <button 
                                class="btn btn-cancel"
                                @click="handleCancel"
                            >
                                取消
                            </button>
                            <button 
                                class="btn btn-confirm"
                                @click="handleConfirm"
                                :disabled="!tempSelectedPath.length"
                            >
                                确定
                            </button>
                        </div>
                    </div>
                </div>
            `
        }

        // 主应用
        createApp({
            components: {
                CascaderSelect
            },
            setup() {
                // 修正后的四层数据结构
                const fourLevelOptions = ref([
                    {
                        name: 'component',
                        nameCn: '组件',
                        nameEn: 'Component',
                        children: [
                            {
                                name: 'basic',
                                nameCn: '基础组件',
                                nameEn: 'Basic',
                                children: [
                                    { 
                                        name: 'button', 
                                        nameCn: '按钮', 
                                        nameEn: 'Button',
                                        children: [
                                            { name: 'button11', nameCn: '按钮11', nameEn: 'Button11' },
                                            { name: 'input11', nameCn: '输入框11', nameEn: 'Input11' }
                                        ] 
                                    },
                                    { 
                                        name: 'input', 
                                        nameCn: '输入框', 
                                        nameEn: 'Input',
                                        children: [
                                            { name: 'button12', nameCn: '按钮12', nameEn: 'Button12' },
                                            { name: 'input112', nameCn: '输入框12', nameEn: 'Input12' }
                                        ]
                                    }
                                ]
                            },
                            {
                                name: 'basic2',
                                nameCn: '基础组件2',
                                nameEn: 'Basic2',
                                children: [
                                    { 
                                        name: 'button2', 
                                        nameCn: '按钮2', 
                                        nameEn: 'Button2',
                                        children: [
                                            { name: 'button21', nameCn: '按钮21', nameEn: 'Button21' },
                                            { name: 'input22', nameCn: '输入框22', nameEn: 'Input22' }
                                        ] 
                                    },
                                    { 
                                        name: 'input2', 
                                        nameCn: '输入框2', 
                                        nameEn: 'Input2',
                                        // 这个没有 children，所以只有3层深度
                                    }
                                ]
                            },
                            {
                                name: 'basic3',
                                nameCn: '基础组件3',
                                nameEn: 'Basic3',
                                children: [
                                    { 
                                        name: 'button3', 
                                        nameCn: '按钮3', 
                                        nameEn: 'Button3'
                                        // 这个没有 children，所以只有3层深度
                                    },
                                    { 
                                        name: 'input3', 
                                        nameCn: '输入框3', 
                                        nameEn: 'Input3'
                                        // 这个没有 children，所以只有3层深度
                                    }
                                ]
                            }
                        ]
                    }
                ])

                // 四层级联选择器的头部标签
                const headerLabels = ['根分类', '子分类', '组件类型', '具体组件']

                // 响应式数据
                const selectedValue = ref([])
                const selectedLabels = ref([])

                // 计算属性 - 最深层级的值（用作请求参数）
                const requestParam = computed(() => {
                    if (!selectedValue.value.length) return ''
                    return selectedValue.value[selectedValue.value.length - 1]
                })

                // 方法
                const updateSelectedValue = (value) => {
                    selectedValue.value = value
                }

                const handleCascaderChange = (value, selectedOptions) => {
                    selectedLabels.value = selectedOptions.map(opt => opt.nameCn)
                    
                    console.log('=== 级联选择器变更 ===')
                    if (value.length === 0) {
                        console.log('🗑️ 已清空选择')
                    } else {
                        console.log('选中路径:', selectedLabels.value.join(' → '))
                        console.log('选择层级:', value.length)
                        console.log('最深层级值 (请求参数):', requestParam.value)
                    }
                    console.log('选中值数组:', value)
                    console.log('选中选项对象:', selectedOptions)
                }

                return {
                    fourLevelOptions,
                    headerLabels,
                    selectedValue,
                    selectedLabels,
                    requestParam,
                    updateSelectedValue,
                    handleCascaderChange
                }
            }
        }).mount('#app')
    </script>
</body>
</html>
